import metchonServices from '@/services/metchonService.js';
import file from '@/common/utils/file.js'

export default {
	data() {
		return {
			sbxxByQrData: {},
			gzlxOptions: [],
			clztOptions: [{
					label: '正常',
					value: 0
				},
				{
					label: '异常',
					value: 1
				}, {
					label: '其他',
					value: 2
				}
			],
			images1: [],
			images2: [],
			fromData: {},
		}
	},
	onShow(){
		
		console.log('weixiu show')
	},
	async onLoad() {
		await this.$store.dispatch('checkLoginAction');
	},
	onTabItemTap : function(e) {
		this.init();
	},
	methods: {
		init(){
			this.sbxxByQrData = {
				yl: "",
				yl1: "",
				yl2: ""
			};
			this.fromData = {
				according: "",
				analysis: "",
				article: "",
				cljg: "",
				clr: "",
				clsj: "",
				cltp1: "",
				clzt: "",
				createBy: "",
				createTime: "",
				gzlx: "",
				gzsm: "",
				gztp1: "",
				id: "",
				programme: "",
				sbid: "",
				sysOrgCode: "",
				updateBy: "",
				updateTime: "",
				yl: "",
				yl1: "",
				yl2: ""
			};
			this.images1 = [];
			this.images2 = [];
		},
		async onSaoMiao() {
			const that = this;
			// that.sbxxByQrData = await metchonServices.querySbxxByQr('0ef3d8c1e6d94d709008805295b551ee');
			// that.gzlxOptions = await metchonServices.getfaultListByType(that.sbxxByQrData.sblx);
			// that.fromData.sbid = that.sbxxByQrData.id;
			// that.fromData.yl = that.sbxxByQrData.yl;
			// that.fromData.yl1 = that.sbxxByQrData.yl1;
			// that.fromData.yl2 = that.sbxxByQrData.yl2;
			// 调起条码扫描
			uni.scanCode({
				onlyFromCamera: false, // 是否只能从相机扫码，不允许从相册选择图片
				scanType: ['qrCode'], // 扫码类型 qrCode二维码
				autoZoom: true, // 是否启用自动放大，默认启用
				success: async function(res) {
					console.log(`扫码结果：${JSON.stringify(res,null,2)}`);
					// res.result;
					that.sbxxByQrData = await metchonServices.querySbxxByQr(res.result);
					that.gzlxOptions = await metchonServices.getfaultListByType(that.sbxxByQrData.sblx);
					that.fromData.sbid = that.sbxxByQrData.id;
					that.fromData.yl = that.sbxxByQrData.yl;
					that.fromData.yl1 = that.sbxxByQrData.yl1;
					that.fromData.yl2 = that.sbxxByQrData.yl2;
				},
				fail: function(err) {
					console.log(`错误：${err}`);
				},
				complete: function(data) {}
			});
		},
		// 获取上传状态
		async onSelect1(obj) {
			const tempFilePaths = obj.tempFilePaths;
			var img = await file.uploadFile(tempFilePaths[0]);
			this.images1.push(img);
		},
		onDeleteFile1(obj) {
			this.images1 = this.images1.filter(e => e.url != obj.tempFilePath);
		},
		// 获取上传状态
		async onSelect2(obj) {
			const tempFilePaths = obj.tempFilePaths;
			var img = await file.uploadFile(tempFilePaths[0]);
			this.images2.push(img);
		},
		onDeleteFile2(obj) {
			this.images2 = this.images2.filter(e => e.url != obj.tempFilePath);
		},
		
		async onSubmit() {
			if (!this.sbxxByQrData || !this.fromData.sbid ) {
				this.$message.error('请扫描设备');
				return;
			}
			if (!this.fromData.gzlx) {
				this.$message.error('请选择故障类型');
				return;
			}
			if (!this.fromData.according) {
				this.$message.error('请输入援引依据');
				return;
			}
			if (!this.fromData.article) {
				this.$message.error('请输入具体条款');
				return;
			}
			if (!this.fromData.analysis) {
				this.$message.error('请输入问题分析');
				return;
			}
			if (!this.fromData.programme) {
				this.$message.error('请输入解决方案');
				return;
			}
			if (!this.fromData.clzt && this.fromData.clzt !== 0) {
				this.$message.error('请选择维修状态');
				return;
			}
			if(!this.images1 || this.images1.length === 0){
				this.$message.error('请上传故障图片');
				return;
			}
			if(!this.images2 || this.images2.length === 0){
				this.$message.error('请上传维修图片');
				return;
			}
			this.fromData.cltp1 = this.images1[0].name;
			this.fromData.gztp1 = this.images2[0].name;
			await metchonServices.faultAdd(this.fromData);
			this.$message.success('添加成功')
		}
	}
}